/* create by fillbag at 2009-03-24 QQ:286116033 */
function xajax_extend()
{
    if(typeof(xajax) == 'undefined')
    {
        return false;
    }
    
    xajax.isIE = (document.all && navigator.appVersion.toLowerCase().indexOf('msie')!=-1) || false;

    xajax.newSessionID = function()
    {
        var sessionID;
        sessionID = new String(new Date().getTime());
        var random = new String(Math.random( )).substr(2);
        sessionID = sessionID + random;
        return sessionID;
    };
    
    // overlay this if you need to
    xajax.setStatusMessages = function(msg)
    {
        window.status = msg;
    };
    
    // overlay this if you need to
    xajax.setCursor = function(cursor)
    {
          if (document.body)
            document.body.style.cursor = cursor;
    };
    
    var loadingTimeout;
    // overlay this if you need to
       xajax.beforeUpload = function()
       {
        // init status
        if(xajaxWaitCursor) xajax.setCursor('wait');
           if(xajaxStatusMessages) xajax.setStatusMessages('Sending Request...');
        clearTimeout(loadingTimeout);
        loadingTimeout = setTimeout("xajax.loadingFunction();",400);
					alert('-------------');
        if (xajaxDebug) xajax.DebugMessage("Starting xajax...");
       };

    xajax.upload = function(rpcFunc,formID,url)
    {
        var form = xajax.$(formID);
        if(!form)
        {
            return false;
        }
        if(form.encoding.toLowerCase() != 'multipart/form-data')
        {
            form.encoding = 'multipart/form-data';
        }
        if(form.method.toLowerCase() != 'post')
        {
            form.method = 'post';
        }
        var newSessionID = xajax.newSessionID();
        
        xajax.beforeUpload();

        if(!url)
        {
            url = xajaxRequestUri;
        }
        var separator = '?';
        if(url.indexOf('?') != -1)separator = '&';
        url += separator + 'xajax='+encodeURIComponent(rpcFunc);
        url += "&xajaxr=" + new Date().getTime();
        // get the upload file local path
        var formItem;
        var nodeName;
        for(var i=0; i<form.getElementsByTagName('input').length; i++)
        {
            formItem = form.elements[i];
            nodeName = new String(formItem.nodeName).toLowerCase();
            if(formItem.name == '' || nodeName == 'button')
            {
                continue;
            }
            if(formItem.type == 'file')
            {
                url += '&'+formItem.name+'='+encodeURIComponent(formItem.value);
            }
        }
        form.action = url;
        
        var iframeName = form.id + newSessionID;
        var iframe;
        if((iframe = xajax.$(iframeName)))
        {
            document.body.removeChild(iframe);
        }
        iframe = xajax.createIframe(iframeName,iframeName);
        form.target = iframeName;
        var xmlDoc;
        var responseXML;
        if(typeof iframe.onreadystatechange == 'object') // for IE
        {
            iframe.onreadystatechange = function()
            {
                if(iframe.readyState == 'complete' && !iframe.loaded)
                {
                    // IE load twice (bug or feature?)
                    iframe.loaded = true;
                    xmlDoc = document.frames(iframe.id);
                    if(xmlDoc.window.document.location != iframe.src)
                    {
                        responseXML = xmlDoc.window.document.XMLDocument;
                        xajax.uploadResponse(responseXML,iframe, xmlDoc);
                    }
                }
            }
        }
        else // for FF
        {
            iframe.onload = function()
            {
                xmlDoc = iframe.contentWindow;
                if(xmlDoc.window.document.location != iframe.src)
                {
                    responseXML = xmlDoc.document;
                    xajax.uploadResponse(responseXML,iframe, xmlDoc);
                }
            }
        }
    };
    
    xajax.uploadResponse = function(responseXML,iframeObj, xmlDoc)
    {
        // response the xml
        var error=false;
        if(responseXML)
        {
            try
            {
                xajax.processResponse(responseXML);
            }
            catch(e)
            {
                error = true;
                //xajax.DebugMessage(e.description);
            }
        }
        else
        {
            error = true;
        }
        
        if(error)
        {
            var responseText = new String(xmlDoc.document.body.innerHTML);
            xajax.uploadFalse(responseText);
        }
        else
        {
            xajax.uploadSuccess();
        }

        // remove the iframe after response
        setTimeout(function(){document.body.removeChild(iframeObj)}, 500);
    };
    
    // overlay this if you need to
    xajax.uploadFalse = function(responseText)
    {
        var errorString = "Error: the XML response that was returned from the server is invalid.";
        errorString += "\nReceived:\n" + responseText;
        var trimmedResponseText = responseText.replace( /^\s+/g, "" );// strip leading space
        trimmedResponseText = trimmedResponseText.replace( /\s+$/g, "" );// strip trailing
        if (trimmedResponseText != responseText)
            errorString += "\nYou have whitespace in your response.";
        xajax.DebugMessage(errorString);
        xajax.setCursor('default');
        if (xajaxStatusMessages == true) xajax.setStatusMessages('Invalid XML response error');
    };
    
    // overlay this if you need to
    xajax.uploadSuccess = function()
    {
        xajax.setCursor('default');
    };
    
    xajax.createIframe = function(name,id)
    {
        var iframe;
        if(!id)
        {
            id = '';
        }
        if(xajax.isIE) // for IE
        {
            iframe = document.createElement('<iframe id="'+id+'" name="'+name+'">');
        }
        else // for FF
        {
            iframe = document.createElement('iframe');
            iframe.id = id;
            iframe.name = name;
        }
        iframe.width = '0';
        iframe.height = '0';
        iframe.style.display = 'none';
        iframe.scrolling = 'no';
        iframe.src = 'about:blank';
        document.body.appendChild(iframe);
        return iframe;
    };
    return true;
}
xajax_extend();